/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package edu.uts.reekie.designer.edit;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;

import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;

import edu.uts.reekie.designer.model.commands.ConnectionCommand;
import edu.uts.reekie.designer.edit.ViewNodeEditPart;
import edu.uts.reekie.designer.figures.FigureFactory;
import edu.uts.reekie.designer.figures.NodeFigure;
import edu.uts.reekie.designer.model.*;


/*
 * 
 * TODO: totally refactor this for creating connectors
 * TODO: move this to common edit pkg
 */
public class ViewElementNodeEditPolicy
	extends org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy
{

	public ViewElementNodeEditPolicy(ModelFactory modelFac, FigureFactory figFac){
		this.modelFactory = modelFac;
		this.figureFactory = figFac;
		if (figFac==null)
			throw new IllegalArgumentException("figure factory is null");
		
		if (modelFac==null)
			throw new IllegalArgumentException("element factory is null");
	}
	
private ModelFactory modelFactory;
private FigureFactory figureFactory;
	
protected Connection createDummyConnection(Request req) {
	PolylineConnection conn = figureFactory.createNewConnector(null);
	return conn;
}

protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {	
	ConnectionCommand command = (ConnectionCommand)request.getStartCommand();
	command.setTarget(getViewNode());
	ConnectionAnchor ctor = getEditPart().getTargetConnectionAnchor(request);
	if (ctor == null)
		return null;
	return command;
}

protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
	int direction = ((Integer) request.getNewObjectType()).intValue();
	ConnectionCommand command = new ConnectionCommand();
	AbstractConnector connector = modelFactory.createConnector();
	
	connector.setDirection(direction);
	command.setConnector(connector);
	command.setSource(getViewNode());
	ConnectionAnchor ctor = getEditPart().getSourceConnectionAnchor(request);
	request.setStartCommand(command);
	return command;
}

/**
 * Feedback should be added to the scaled feedback layer.
 * @see org.eclipse.gef.editpolicies.GraphicalEditPolicy#getFeedbackLayer()
 */
protected IFigure getFeedbackLayer() {
	/*
	 * Fix for Bug# 66590
	 * Feedback needs to be added to the scaled feedback layer
	 */
	return getLayer(LayerConstants.SCALED_FEEDBACK_LAYER);
}



protected ViewNodeEditPart getEditPart() {
	return (ViewNodeEditPart) getHost();
}

protected ViewNode getViewNode() {
	return (ViewNode) getHost().getModel();
}

protected Command getReconnectTargetCommand(ReconnectRequest request) {
	
	ConnectionCommand cmd = new ConnectionCommand();
	cmd.setConnector((AbstractConnector)request.getConnectionEditPart().getModel());

	ConnectionAnchor ctor = getEditPart().getTargetConnectionAnchor(request);
	cmd.setTarget(getViewNode());
	return cmd;
}

protected Command getReconnectSourceCommand(ReconnectRequest request) {
	ConnectionCommand cmd = new ConnectionCommand();
	cmd.setConnector((AbstractConnector)request.getConnectionEditPart().getModel());

	ConnectionAnchor ctor = getEditPart().getSourceConnectionAnchor(request);
	cmd.setSource(getViewNode());
	return cmd;
}

protected NodeFigure getNodeFigure() {
	return (NodeFigure)((GraphicalEditPart)getHost()).getFigure();
}

}
